#include <iostream>
#include <sstream>
#include <string>
#include <queue>
using namespace std;

struct Node {
	string val;
	Node* left = nullptr;
	Node* right = nullptr;
};
string sp;
Node* A = nullptr, *B = nullptr;
void getTree(Node*& h)
{
	h = new Node;
	queue<Node*> q;
	string line;
	getline(cin, line);
	stringstream ss(line);
	ss >> h->val;
	q.push(h);
	while (q.size()) {		
		Node* cur = q.front(); q.pop();
		string l, r;
		if (!(ss >> l))break;
		if (l != sp) {
			cur->left = new Node;
			cur->left->val = l;
			q.push(cur->left);
		}
		if (!(ss >> r))break;
		if (r != sp) {
			cur->right = new Node;
			cur->right->val = r;
			q.push(cur->right);
		}
	}
}

bool same(Node* a, Node* b)
{
	if (b == nullptr) return true;
	if (a == nullptr) return false;
	if (a->val != b->val) return false;
	return same(a->left, b->left) && same(a->right, b->right);
}

bool jug(Node* a, Node* b)
{
	if (b == nullptr) return true;
	if (a == nullptr) return false;
	if (a->val == b->val && same(a, b)) return true;
	return jug(a->left, b) || jug(a->right, b);
}

int main()
{
	getline(cin, sp);
	getTree(A);
	getTree(B);
	
	if (jug(A, B)) cout << "true";
	else cout << "false";

	return 0;
}